/*
 * Copyright (c) 2021 柯华栋
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef IPC_SERVER_H
#define IPC_SERVER_H

#include "ipc_util.h"

typedef void 		(*controller_on_post)(ipc_map_t* map);
typedef ipc_map_t* 	(*controller_on_send)(ipc_map_t* map);
typedef void 		(*controller_handle_update)(GDBusConnection* connection, const gchar* id, gint64 observer);

typedef struct
{
	controller_on_post 			on_post;
	controller_on_send 			on_send;
	controller_handle_update 	handle_update;
} module_controller_t;

typedef module_controller_t* (*module_controller_getter)(const gchar* module_name);


/** 设置控制器工厂 */
void server_set_module_controller_getter(module_controller_getter getter);

/** 创建服务 */
GDBusServer* server_new(const gchar* server_name);

// (控制器中的便利函数)
controller_on_post 			id_pointer_map_get_on_post(id_pointer_map_t* id_pointer_map, ipc_map_t* map);
controller_on_send 			id_pointer_map_get_on_send(id_pointer_map_t* id_pointer_map, ipc_map_t* map);
controller_handle_update 	id_pointer_map_get_handle_update(id_pointer_map_t* id_pointer_map, const gchar* id);

// 更新给和id关联的所有connection
void server_update(ipc_map_t* map);

// 更新给指定connection
void server_update_con(GDBusConnection* connection, ipc_map_t* map);

// TODO. ipc_update和ipc_connection_update的其他衍生函数

void server_update_u8(const gchar* id, uint8_t value);
void server_update_s16(const gchar* id, int16_t value);
void server_update_s32(const gchar* id, int32_t value);
void server_update_s64(const gchar* id, int64_t value);
void server_update_float(const gchar* id, float value);
void server_update_double(const gchar* id, double value);
void server_update_string(const gchar* id, const gchar* value);

void server_update_u8_array(const gchar* id, ipc_u8_array_t* array);
void server_update_s16_array(const gchar* id, ipc_s16_array_t* array);
void server_update_s32_array(const gchar* id, ipc_s32_array_t* array);
void server_update_s64_array(const gchar* id, ipc_s64_array_t* array);
void server_update_float_array(const gchar* id, ipc_float_array_t* array);
void server_update_double_array(const gchar* id, ipc_double_array_t* array);
void server_update_string_array(const gchar* id, ipc_string_array_t* array);


void server_update_con_observer_u8(GDBusConnection* connection, const gchar* id, gint64 observer, uint8_t value);
void server_update_con_observer_s16(GDBusConnection* connection, const gchar* id, gint64 observer, int16_t value);
void server_update_con_observer_s32(GDBusConnection* connection, const gchar* id, gint64 observer, int32_t value);
void server_update_con_observer_s64(GDBusConnection* connection, const gchar* id, gint64 observer, int64_t value);
void server_update_con_observer_float(GDBusConnection* connection, const gchar* id, gint64 observer, float value);
void server_update_con_observer_double(GDBusConnection* connection, const gchar* id, gint64 observer, double value);
void server_update_con_observer_string(GDBusConnection* connection, const gchar* id, gint64 observer, const gchar* value);

void server_update_con_observer_u8_array(GDBusConnection* connection, const gchar* id, gint64 observer, ipc_u8_array_t* array);
void server_update_con_observer_s16_array(GDBusConnection* connection, const gchar* id, gint64 observer, ipc_s16_array_t* array);
void server_update_con_observer_s32_array(GDBusConnection* connection, const gchar* id, gint64 observer, ipc_s32_array_t* array);
void server_update_con_observer_s64_array(GDBusConnection* connection, const gchar* id, gint64 observer, ipc_s64_array_t* array);
void server_update_con_observer_float_array(GDBusConnection* connection, const gchar* id, gint64 observer, ipc_float_array_t* array);
void server_update_con_observer_double_array(GDBusConnection* connection, const gchar* id, gint64 observer, ipc_double_array_t* array);
void server_update_con_observer_string_array(GDBusConnection* connection, const gchar* id, gint64 observer, ipc_string_array_t* array);



#endif // IPC_SERVER_H
